package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.Deck;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.utils.Constants;

public class Workshop extends Action {

	public Workshop() {
		cost = 3;
	}

	@Override
	public Card clone() {
		return new Workshop();
	}

	@Override
	public String toString() {
		return "Workshop";
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("Gain a card costing up to $4\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		player.changeAction(-1);
		playedCard(player);

		Constants.printer.print("Pick a card costing at most $4");
		Constants.printer.printBoard(board);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		boolean done = false;
		String choice = null;
		while (!done)
		{
			choice = in.readLine().trim().toLowerCase();
			Card card = Constants.cardMap.get(choice);
			if (card == null)
			{
				Constants.printer.print("Not a valid card");
			}
			else if (card.getCost() > 4)
			{
				Constants.printer.print("Card is too expensive. Must pick a card costing 4 or less.");
			}
			else
			{
				Deck deck = board.getMap().get(choice);
				if (deck.isEmpty())
				{
					Constants.printer.print("That pile is empty.");
				}
				else
				{
					player.addToDeck(deck.drawTopCard(), Constants.DISCARD_DECK);
					done = true;
				}
			}
		}
	}
}
